package com.asurplus.weixin.service.impl;

import com.asurplus.common.utils.DateUtils;
import com.asurplus.weixin.message.resp.TextMessage;
import com.asurplus.weixin.service.WeiXinService;
import com.asurplus.weixin.service.WeixinSubscribeLogService;
import com.asurplus.weixin.utils.MessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

@Slf4j
@Service
public class WeiXinServiceImpl implements WeiXinService {

    @Autowired
    private WeixinSubscribeLogService weixinSubscribeLogService;

    @Override
    public String processRequest(HttpServletRequest request) {
        // xml格式的消息数据
        String respXml = null;
        try {
            // 调用parseXml方法解析请求消息
            Map<String, String> requestMap = MessageUtil.parseXml(request);
            // 发送方帐号，实则openId
            final String fromUserName = requestMap.get("FromUserName");
            // 开发者微信号
            String toUserName = requestMap.get("ToUserName");
            // 消息类型
            String msgType = requestMap.get("MsgType");
            // 回复文本消息
            TextMessage textMessage = new TextMessage();
            textMessage.setToUserName(fromUserName);
            textMessage.setFromUserName(toUserName);
            Date date = new Date();
            textMessage.setCreateTime(date.getTime());
            textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT);
            String content = null;
            // 事件推送
            if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)) {
                // 事件类型
                String eventType = requestMap.get("Event");
                // 关注
                if (eventType.equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)) {
                    StringBuffer str = new StringBuffer();
                    str.append("就这样爱你爱你爱你随时都要一起\n");
                    content = str.toString();
                    log.info("{} 关注公众号", fromUserName);
                    // 保存日志记录
                    weixinSubscribeLogService.saveWeixinSubscribeLog(fromUserName, 0);
                }
                // 取消关注
                else if (eventType.equals(MessageUtil.EVENT_TYPE_UNSUBSCRIBE)) {
                    log.info("{} 取消关注公众号", fromUserName);
                    // 保存日志记录
                    weixinSubscribeLogService.saveWeixinSubscribeLog(fromUserName, 1);
                }
                // 点击事件
                else if (eventType.equals(MessageUtil.EVENT_TYPE_CLICK)) {
                    String eventKey = requestMap.get("EventKey");
                    // 最新回复
                    if (eventKey.equals("V1001_TODAY_MUSIC")) {
                        StringBuffer str = new StringBuffer();
                        str.append("这是一条最新回复信息，" + DateUtils.getYmdHmsZh());
                        content = str.toString();
                        log.info("{} 点击事件", fromUserName);
                    }
                }
            }
            // 消息回复
            else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) {
                String msgContent = requestMap.get("Content");
                if (msgContent.equals("1")) {
                    StringBuffer str = new StringBuffer();
                    str.append("您发送的消息为：" + msgContent);
                    content = str.toString();
                    log.info("{} 消息回复", fromUserName);
                }
            }
            // 消息对象转换
            if (StringUtils.isNotBlank(content)) {
                // 设置文本消息的内容
                textMessage.setContent(content);
                // 将文本消息对象转换成xml
                respXml = MessageUtil.messageToXml(textMessage);
            }
        } catch (Exception e) {
            log.error("{}", e);
        }
        return respXml;
    }
}
